/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package business;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *  obsahuje data o pocasi, naplni se daty v YRXmlHandleru
 * @author stana
 */
public class YRWeatherForecast
{
    //klic je String ve formatu YYYY-MM-DD

    private Map<String, YRDay> days = new HashMap<String, YRDay>();

    public void addDay(String day, YRDay yrday)
    {
        days.put(day, yrday);
    }

    public boolean haveDay(String day)
    {
        return days.containsKey(day);
    }

    public String getForecast(String day,int hour)//zatim jenom vypis pro testovani
    {

        YrHour h=days.get(day).getHour(hour);
        return h.getWindSpeed()+", "+h.getWindDirection()+", "+h.getPrecipitation()+", "
                +h.getTemperature()+", "+h.getPressure();
    }
    /**
     * predpoved na dva dny,
     * bude to vypadat asi takhle:
     *  MM-DD:h-h:x.xm/sWS,x.xmm,x.xC,
     * @return
     */
    public String twoDayForecast()
    {
        String ret="";
        Calendar c=Calendar.getInstance();/*ulozi do c dnesni datum */
        c.add(Calendar.DAY_OF_MONTH,1);  /*zmeni datum c na zitra */

        String day=calendarToString(c);//prevede c na String YYYY-MM-DD
        ret+=day.substring(5);//prida do vysledku "MM-DD:0-"
        //System.out.println(day);
        YrHour h=null;//days.get(day).getHour(0);
        for(int i=0;i<=18;i=i+6)
        {
            ret+=";"+i+"h";
            ret+=precipitationAvg(day, i, i+6);
            h=days.get(day).getHour(i);
            ret+="mm"+h.getWindSpeed()+"m/s"+h.getWindDirection()+h.getTemperature()+"C";

        }
//        return h.getWindSpeed()+", "+h.getWindDirection()+", "+h.getPrecipitation()+", "
//                +h.getTemperature()+", "+h.getPressure();
        return ret;
    }
    /**
     * vrati prumer srazek v danem obdobi
     * @param date
     * @param begin
     * @param end
     * @return
     */
    private double precipitationAvg(String date,int begin,int end)
    {
        double sum=0;
        YrHour h=null;
        for(int i=begin;i<end;i++)
        {
            h=days.get(date).getHour(i);
            //System.out.println("i"+i+h.getPrecipitation());
            sum+=h.getPrecipitation();
        }
        return (Math.round((sum/(end-begin)*100)))/100.0; //zaokrouhleni na dve mista
        
    }

   /**
    * prevede calendarove datum na String ve formatu YYYY-MM-DD
    * @param c
    * @return
    */
   private String calendarToString(Calendar c)
   {
       int day=c.get(Calendar.DAY_OF_MONTH);
       String dayS=String.valueOf(day);
       if(day<10)//cislo musi byt vzdy dvouciferne(napr. 01)
       {
           dayS="0"+dayS;
       }
       int month=c.get(Calendar.MONTH)+1;//cislo mesice(mesice jsou od nuly, proto +1)
       String monthS=String.valueOf(month);
       if(month<10)//cislo musi byt vzdy dvouciferne(napr. 01)
       {
           monthS="0"+monthS;
       }
       return c.get(Calendar.YEAR)+"-"+monthS+"-"+dayS;

   }


    //kvuli debugovani
    @Override
    public String toString()
    {
        String ret = "";
        YrHour hour;
        Collection<YRDay> c = days.values();
        for (YRDay d : c)
        {
            ret += "\n" + d.getDate();
            for (int i = 0; i < 24; i++)
            {
                hour = d.getHour(i);
                if (hour != null)
                {
                    ret = ret + "\n   i:"+i+"hour: " + hour.getHour() + "-windDirection: " + hour.getWindDirection()
                            + ",windSpeed: " + hour.getWindSpeed() + ",precipitation+ " + hour.getPrecipitation();
                }
            }


        }
        return ret;
    }
}